package tests;

/**
 * @author Team TA's
 */

import java.util.ArrayList;
import java.util.HashMap;

import junit.framework.TestCase;
import org.junit.*;

import fi.helsinki.cs.KaiSei.*;
import fi.helsinki.cs.KaiSei.Weekday.Day;

public class ScheduleTest extends TestCase {

        Schedule schedule;
        ArrayList<Day> week;
        ArrayList<Event> events;
        ArrayList<ArrayList<Event>> map;

        @Before
        public void setUp() throws Exception {
                week = new ArrayList<Day>();
                week.add(Day.MON);
                week.add(Day.TUE);
                week.add(Day.WED);
                week.add(Day.THU);

                events = new ArrayList<Event>();
                events.add(new Event(8, 12, "first", "firstLocation"));
                events.add(new Event(18, 20, "second", "secondLocation"));
                events.add(new Event(12, 14, "third", "thirdLocation"));
                events.add(new Event(10, 16, "fourth", "fourthLocation"));
                events.add(new Event(14, 18, "fifth", "fifthLocation"));
                events.add(new Event(8, 20, "sixth", "sixthLocation"));

                map = new ArrayList<ArrayList<Event>>();
                for (int i=0;i<7;i++){
                        map.add(events);
                }

        }

        @After
        public void tearDown() throws Exception {
                map = null;

                events = null;

                week = null;            

                // if test case forgots to set schedule to null...
                if (schedule != null){
                        schedule = null;
                }
        }


        @Test
        public void testAllConstructors(){
                schedule = new Schedule();

                assertNotNull(schedule);

                for (ArrayList<Event> e : schedule.getEventList()){
                        assertNotNull(e);
                }
                schedule = null;

                assertNull(schedule);

                String testperiod = "testperiod";
                schedule = new Schedule(testperiod);

                assertNotNull(schedule);

                for (ArrayList<Event> e : schedule.getEventList()){
                        assertNotNull(e);
                }

                assertNotNull(schedule.getPeriod());
                assertEquals(testperiod, schedule.getPeriod());
                schedule = null;
        }

        @Test
        public void testAllSetSchedules(){
                schedule = new Schedule("first period");
                for(Event e:events){
                        for(int d=0;d<7;d++){
                                schedule.addEvent(d, e);
                        }
                }
                assertEquals(map, schedule.getEventList());

//              Schedule anotherSchedule = new Schedule();
//              anotherSchedule.setSchedule(schedule);

        }

        @Test
        public void testSetPeriod(){
                String period = "first period";
                schedule = new Schedule(period); //WTF

                assertEquals(period, schedule.getPeriod());

                period = "totally another period";
                schedule.setPeriod(period);
                assertEquals(period, schedule.getPeriod());
        }

        @Test
        public void testAddEvent(){
                schedule = new Schedule();
                Event e = new Event(16, 18, "testAddEvent", "testAddEventLocation");
                schedule.addEvent(4, e);

                assertEquals(1, schedule.getEventList(4).size());
                assertEquals(e, schedule.getEventList(4).get(0));
        }

        @Test
        public void testAddEventWhenWrongDay(){
                schedule = new Schedule("period");

                try {
                        schedule.addEvent(7, events.get(0));
                        fail("Expecting IllegalArgumentException when no such day in schedule");
                } catch (IllegalArgumentException e) {
                        // Everything went better than expected
                }

        }

}
